//
//  CheckAPPSetting.m
//  Qianbancaifu
//
//  Created by SYLH on 16/1/8.
//  Copyright © 2016年 SYLH. All rights reserved.
//

#import "CheckAPPSetting.h"


@implementation CheckAPPSetting

#pragma mark - 检查是否允许推送消息
+ (BOOL)isAllowedNotification {
    //iOS8 check if user allow notification
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {// system is iOS8
        UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
        if (UIUserNotificationTypeNone != setting.types) {
            return YES;
        }
    }
//    else {//iOS7
//        UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
//        if(UIRemoteNotificationTypeNone != type)
//            return YES;
//    }
    return NO;
}

#pragma mark - 检查是否打开通讯录权限
+(void)CheckAddressBookAuthorization:(void (^)(bool isAuthorized))block
{
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus();
    
    if (authStatus != kABAuthorizationStatusAuthorized)
    {
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error)
                                                 {
                                                     dispatch_async(dispatch_get_main_queue(), ^{
                                                         if (error)
                                                         {
                                                             NSLog(@"Error: %@", (__bridge NSError *)error);
                                                             block(NO);
                                                         }
                                                         else if (!granted)
                                                         {
                                                             
                                                             block(NO);
                                                         }
                                                         else
                                                         {
                                                             block(YES);
                                                         }
                                                     });
                                                 });
    }else{
        block(YES);
    }
    addressBook = nil;
}
#pragma mark - 检查相册权限
+(void) checkPhotos:(void (^)(bool isAuthorized))block
{
    ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
    if (author == ALAuthorizationStatusRestricted || author ==ALAuthorizationStatusDenied){
        block(NO);
    }else{
        block(YES);
    }
}

#pragma mark - 检查相机权限
+(void) checkCamera:(void (^)(bool isAuthorized))block
{
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
    {
        block(NO);
    }else{
        block(YES);
    }
}

#pragma mark - 检查麦克风权限
+(void)checkMicrophone:(void (^)(bool isAuthorized))block
{
    AVAudioSession *avSession = [AVAudioSession sharedInstance];
    
    if ([avSession respondsToSelector:@selector(requestRecordPermission:)]) {
        
        [avSession requestRecordPermission:^(BOOL available) {
            
            if (available) {
                block(YES);
            }
            else
            {
               block(NO);
            }
        }];
        
    }
}

+(BOOL)currentLanguageIsChinese
{
    NSArray *languages = [NSLocale preferredLanguages];
    NSString *currentLanguage = [languages objectAtIndex:0];
    
    if ([currentLanguage  rangeOfString:@"zh-Hans"].length > 0 || [currentLanguage  rangeOfString:@"zh-Hant"].length > 0) {
        return YES;
    }
    return NO;
}

@end
